Xbasic

SQL::ResultSetToJSON Method

Syntax

JSON as C = toJSON([RowsToCopy = -1 as N [, StartRow = -1 as N [, UpperCaseNames = .f. as L [, ConvertToText = .t. as L [, DateTimeFormat = "" as C [, DateFormat = "" as C [, ReferenceColumns as SQL::TableInfo [, UserContext as P]]]]]]]])

Arguments

RowsToCopyNumeric

Default = -1. The number of records to convert to JSON from the ResultSet. If -1, all records will be converted.

StartRowNumeric

Default = -1. The row number for the first record to use from the ResultSet. The ToJSON() method can be called after some records have already been read from the result set. If StartRow is -1, the next available record row will be the first converted to JSON. If you specify a row that has already been read, this method can fail.

UpperCaseNamesLogical

Default = .f.. Whether or not the JSON properties should be uppercase. If .t., names will be uppercase.

ConvertToTextLogical

Default = .t.. If .T., single-quote numeric and logical values

DateTimeFormatCharacter

Default = "". Format string for dateTime values, as used by the time Function, e.g. "yyyy-MM-dd". A value of "" means use the default format.

DateFormatCharacter

Default = "". Format string for date values, as used by the time Function. A value of "" means use the default format.

ReferenceColumnsSQL::TableInfo

Default = null_value(). When data is formatted for a column in the result set: (1) if ReferenceColumns has a column with a matching name, that object will be used to format the data; (2) otherwise the ColumnInfo property of the result set is used to format the data.

UserContextPointer

Default = null_value(). The user context is passed into the evaluation of the expression when data is formatted.

Returns

JSONCharacter

Returns the SQL::ResultSet in a JSON format.

Description

Convert a ResultSet to a JSON formatted string.

Discussion

The .toJSON() method creates a parsable JSON array of objects.

Contrast the AlphaDAO SQL::ResultSet.toJSON() with the AlphaDAO SQL::ResultSet.toJSONObjectSyntax() method. The later generates a CR-LF delimited string of JSON objects. To turn the output from this method into a parsable string, it is necessary to add the opening and closing square brackets and to terminate each line, except the last, with a comma.

Examples:

dim cn as sql::Connection
cn.Open("::Name::northwind")
cn.Execute("select customerid, contactname, city, country from customers where country = 'France'")
dim json as c = cn.resultSet.tojson()
? convert_utf8_to_acp(json)
= [
{"CustomerID" : "BLONP", "ContactName" : "Frédérique Citeaux", "City" : "Strasbourg", "Country" : "France"},
{"CustomerID" : "BONAP", "ContactName" : "Laurence Lebihan", "City" : "Marseille", "Country" : "France"},
{"CustomerID" : "DUMON", "ContactName" : "Janine Labrune", "City" : "Nantes", "Country" : "France"},
{"CustomerID" : "FOLIG", "ContactName" : "Martine Rancé", "City" : "Lille", "Country" : "France"},
{"CustomerID" : "FRANR", "ContactName" : "Carine Schmitt", "City" : "Nantes", "Country" : "France"},
{"CustomerID" : "LACOR", "ContactName" : "Daniel Tonini", "City" : "Versailles", "Country" : "France"},
{"CustomerID" : "LAMAI", "ContactName" : "Annette Roulet", "City" : "Toulouse", "Country" : "France"},
{"CustomerID" : "PARIS", "ContactName" : "Marie Bertrand", "City" : "Paris", "Country" : "France"},
{"CustomerID" : "SPECD", "ContactName" : "Dominique Perrier", "City" : "Paris", "Country" : "France"},
{"CustomerID" : "VICTE", "ContactName" : "Mary Saveley", "City" : "Lyon", "Country" : "France"},
{"CustomerID" : "VINET", "ContactName" : "Paul Henriot", "City" : "Reims", "Country" : "France"}
]


cn.Execute("select customerid, contactname, city, country from customers where country = 'Spain'")
' Note that toJSONObjectSyntax does not include commas or brackets:
json = cn.resultSet.ToJSONObjectSyntax() 
? convert_utf8_to_acp(json)
= {"CustomerID" : "BOLID", "ContactName" : "Martín Sommer", "City" : "Madrid", "Country" : "Spain"}
{"CustomerID" : "FISSA", "ContactName" : "Diego Roel", "City" : "Madrid", "Country" : "Spain"}
{"CustomerID" : "GALED", "ContactName" : "Eduardo Saavedra", "City" : "Barcelona", "Country" : "Spain"}
{"CustomerID" : "GODOS", "ContactName" : "José Pedro Freyre", "City" : "Sevilla", "Country" : "Spain"}
{"CustomerID" : "ROMEY", "ContactName" : "Alejandra Camino", "City" : "Madrid", "Country" : "Spain"}

See Also